package com.um.jdy.app.shop.manager;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.um.jdy.app.shop.entity.dto.CartChangeNumDTO;
import com.um.jdy.app.shop.entity.dto.JoinCartDTO;
import com.um.jdy.app.shop.entity.vo.CartDetailVO;
import com.um.jdy.common.member.entity.po.MemberUser;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.jdy.common.shop.code.ShopGoodsCode;
import com.um.jdy.common.shop.code.ShopOrderCode;
import com.um.jdy.common.shop.entity.po.Cart;
import com.um.jdy.common.shop.entity.po.GoodsOption;
import com.um.jdy.common.shop.entity.vo.GoodsVO;
import com.um.jdy.common.shop.service.CartService;
import com.um.jdy.common.shop.service.GoodsOptionService;
import com.um.springboot.starter.exception.ServiceException;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
public class CartManager {

    @Autowired
    CartService cartService;

    @Autowired
    MemberUserService userService;

    @Autowired
    GoodsManager goodsManager;
    
    @Autowired
    GoodsOptionService goodsOptionService;

    /**
     * 查询购物车
     * @return
     */
    public List<CartDetailVO> get(){
        List<CartDetailVO> list = cartService.getViewModelList("get_cart_goods_list", CartDetailVO.class,
                new String[]{userService.get().getMemberUserId(),userService.getComId()});
        list.stream().forEach(item -> {
            if(StrUtil.isEmpty(item.getThumb()) && StrUtil.isNotEmpty(item.getGoodsThumb())){
                item.setThumb(JSONUtil.parseArray(item.getGoodsThumb()).get(0, String.class));
            }
        });
        return list;
    }

    /**
     * 加入购物车
     * @param dto
     */
    public void join(JoinCartDTO dto){
        MemberUser user = userService.get();

        Cart cart = cartService.getEntity(new String[]{"member_user_id", "shop_goods_id", "shop_goods_option_id"},
                new String[]{user.getMemberUserId(), dto.getShopGoodsId(), dto.getShopGoodsOptionId()});

        //添加购物车商品
        if (null == cart) {
            cart = new Cart();
            cart.setMemberUserId(user.getMemberUserId());
            cart.setShopGoodsId(dto.getShopGoodsId());
            cart.setShopGoodsOptionId(dto.getShopGoodsOptionId());
            cart.setNum(0);
            cart.setCreateTime(DateUtil.date());
        }

        //已存在商品规格就增加数量
        cart.setNum(cart.getNum() + dto.getNum());

        cartService.save(cart);
    }

    /**
     * 修改购物车商品数量
     * @param dto
     */
    public void changeNum(CartChangeNumDTO dto){
        MemberUser user = userService.get();

        Cart cart = cartService.getEntity(new String[]{"member_user_id", "shop_cart_id"},
                new String[]{user.getMemberUserId(), dto.getShopCartId()});
        if(null == cart){
            throw new ServiceException(ShopGoodsCode.CartGoodsNotExist);
        }
        cart.setNum(dto.getNum());

        GoodsOption option = goodsOptionService.getEntity(cart.getShopGoodsOptionId());
        if(null != option && option.getStock() < cart.getNum()){
            throw new ServiceException(ShopOrderCode.StockLack);
        }

        cartService.update(cart);
    }

    /**
     * 移除购入车
     * @param ids
     */
    public void delete(List<String> ids){
        MemberUser user = userService.get();
        ids.stream().forEach(id -> cartService.delete(new String[]{"member_user_id", "shop_cart_id"},
                new String[]{user.getMemberUserId(), id}));
    }

    /**
     * 查询推荐商品
     */
    public List<GoodsVO> getRecommendGoods(){
        List<CartDetailVO> carts = get();
        String codeStr = carts.stream().map(item -> item.getCode()).collect(Collectors.joining(","));
        return goodsManager.getRecommendGoods(codeStr);
    }

}
